<?php

header ('Content-type: text/html; charset=UTF-8'); 


?>

<script src="system.js"></script>
<script src="hmac-md5.js"></script>


<script language="javascript" type="text/javascript">

			
	function submitbutton(pressbutton) 
	{
		//var form = document.adminForm;
	
		switch( pressbutton )
		{
			
			case	"save":
			{
				
					
					//form.btnsave.disabled = true;					
					//form.task.value=pressbutton;

					/*
					var oObject = {} ;
					
					oObject["id"] = getInputValueElementId ("id" );
					oObject["db_port"] = getInputValueElementId( "db_port" );
					oObject["name_view"] = getInputValueElementId( "name_view" );
					oObject["db_hostip"] = getInputValueElementId( "db_hostip" );
					oObject["db_name"] = getInputValueElementId( "db_name" );
					oObject["db_type"] = getInputValueElementId( "db_type" );
					oObject["db_user"] = getInputValueElementId( "db_user" );
					oObject["db_password"] = getInputValueElementId( "db_password" );
					oObject["detail"] = getInputValueElementId( "detail" );
					oObject["id_time_update_data"] = getInputValueElementId( "id_time_update_data" );
					oObject["db_query"] = encodeURIComponent(getInputValueElementId( "db_query" ));
					oObject["json_rule"] = encodeURIComponent(getInputValueElementId( "json_rule" ));
					oObject["component"] = "etl";
					oObject["controller"] = "view";
					oObject["task"] = "save";
					
					
					var jsonVales	=	convertToJSON(oObject);
					
					parameters	=	'parameters='+encodeURIComponent(
											convertToStringJSON(jsonVales));
					
					var method='';
					
					if( getInputValueElementId( "id" ) =='')
						method='POST';
					else
						method='PUT';
					*/	
					
					var oObject = {} ;
					
					
					oObject["db_port"] = getInputValueElementId( "db_port" );					
					oObject["db_hostip"] = getInputValueElementId( "db_hostip" );
					oObject["db_name"] = getInputValueElementId( "db_name" );
					oObject["db_type"] = getInputValueElementId( "db_type" );
					oObject["db_user"] = getInputValueElementId( "db_user" );
					oObject["db_password"] = getInputValueElementId( "db_password" );
					oObject["db_query"] = encodeURIComponent(getInputValueElementId( "db_query" ));
					
					
					var jsonVales	=	convertToJSON(oObject);
					
					json_rules	=	convertToStringJSON(jsonVales);
					
					oObject=null;
					oObject={};
					
					
					oObject["id"] = getInputValueElementId ("id" );
					oObject["name_view"] = getInputValueElementId( "name_view" );
					oObject["detail"] = getInputValueElementId( "detail" );
					oObject["id_time_update_data"] = getInputValueElementId( "id_time_update_data" );
					oObject["rule_connetor"] = json_rules;
					oObject["rule_type"] = getInputValueElementId( "rule_type" );
					oObject["component"] = "etl";
					oObject["controller"] = "view";
					oObject["task"] = "save";
					
					//document.getElementById("json_rule").value=json_rules;
					
					var jsonVales	=	convertToJSON(oObject);
					
					parameters	=	encodeURIComponent(
											convertToStringJSON(jsonVales));
											
					
					var method='';
					
					if( getInputValueElementId( "id" ) =='')
						method='POST';
					else
						method='PUT';
						
					httpRequest ( method,
									url_API,
									parameters,
									'retornoEditItemView');
					
			
						
					return false;
				
				
			}break;
			case	"delete":
			{
				

					var oObject = {} ;
					
					oObject["id"] = getInputValueElementId ("id" );
					oObject["component"] = "etl";
					oObject["controller"] = "view";
					oObject["task"] = "delete";
					
					
					var jsonVales	=	convertToJSON(oObject);
					
					parameters	=	encodeURIComponent(
											convertToStringJSON(jsonVales));
					
					var method='';
					
					if( getInputValueElementId( "id" ) !=''){ 
						
						method='DELETE';
						
						httpRequest ( method,
									url_API,
									parameters,
									'text-result');
					}


		
					return false;
				
				
			}break;
			
			default:
			{
				
			}
		}	
		
	}


function retornoEditItemView(jsonObj){
		
	if( jsonObj.status == 1){
		
		alert( jsonObj.data );
		window.location.href='view_list.php';
			
	}else{
		
		alert( jsonObj.data );
	}

}







function returnItemData(jsonObj){
	

	if( jsonObj.status == 1){

		var obj_json = JSON.parse(jsonObj.data);
		
		var obj_json_rule = JSON.parse(obj_json[0].rule_connector);
		
		
		document.getElementById("id").value = obj_json[0].id;
		document.getElementById("name_view").value = obj_json[0].name_view;
		document.getElementById("detail").value = obj_json[0].detail;
		document.getElementById("table_name").value = obj_json[0].table_name;
		document.getElementById("rule_type").value = obj_json[0].rule_type;

		
		document.getElementById("id_time_update_data").value = obj_json[0].id_time_update_data ;
		//document.getElementById("json_rule").value = obj_json[0].json_rule;
		
		
		document.getElementById("db_hostip").value = obj_json_rule.db_hostip;
		document.getElementById("db_name").value = obj_json_rule.db_name;
		document.getElementById("db_type").value = obj_json_rule.db_type;
		document.getElementById("db_user").value = obj_json_rule.db_user;
		document.getElementById("db_password").value = obj_json_rule.db_password;
		document.getElementById("db_port").value = obj_json_rule.db_port;
		document.getElementById("db_query").value = decodeURIComponent(obj_json_rule.db_query);
		
	}else{

		alert( jsonObj.data );
	}

}



function getItem(id) 
{

	if( id ==="null" 
		|| id===null 
		|| id==="" 
		|| typeof id === "undefined"){

		
	}else{

		var oObject = {} ;
						
		oObject["id"] = id;
		oObject["component"] = "etl";
		oObject["controller"] = "view";
		oObject["task"] = "getItem";

		var jsonVales	=	convertToJSON(oObject);

		parameters	=	encodeURIComponent(
								convertToStringJSON(jsonVales));

		var method='GET';

		httpRequest ( method,
						url_API,
						parameters,
						'returnItemData');
	}


}


</script>

<h1>Editar Visão</h1>
<div id="text-result-view" style="visibility:hidden;width:1px;height:1px;"></div>
<div id="text-result-hidden" style="visibility:hidden;width:1px;height:1px;"></div>
<div id="text-result-hidden-username" style="visibility:hidden;width:1px;height:1px;"></div>

<div id="text-result">Area restria aos usuarios autenticados. voce esta logado como <div id="username-id"></div>.<a href="#" onclick="javascript:logout();">Clique aqui para Logout</a></div>
 
<br>


<a href="home_authenticated.php">Página inicial</a><br>
<a href="view_list.php">Lista de Visões</a><br><br>



<form class="cmxform" action="#" method="GET">
	<input type="hidden" name="parameters" id="parameters" value=''>	

	<div id="label">id</div> <div id="field"><input type="text" id="id" name="id" disabled=""/></div>
	<div id="label">nome</div> <div id="field"><input type="text" id="name_view" name="name_view"/></div>
	<div id="label">detalhe</div> <div id="field"><input type="text" id="detail" name="detail"/></div>
	<div id="label">table</div> <div id="field"><input type="text" id="table_name" name="table_name" disabled=""/></div>
	<div id="label">hostip</div> <div id="field"><input type="text" id="db_hostip" name="db_hostip" /></div>
	<div id="label">dbname</div> <div id="field"><input type="text" id="db_name" name="db_name" /></div>
	<div id="label">dbtype</div> <div id="field"><input type="text" id="db_type" name="db_type" value="postgresql" /></div>
	<div id="label">user</div> <div id="field"><input type="text" id="db_user" name="db_user" /></div>
	<div id="label">password</div> <div id="field"><input type="text" id="db_password" name="db_password" /></div>
	<div id="label">port</div> <div id="field"><input type="text" id="db_port" name="db_port" /></div>
	<div id="label">id_time_update_data (use valores: 1=tempo real,2=1 vez por dia, 3=1 vez por semana, 4=não atualizar)</div> <div id="field"><input type="text" id="id_time_update_data" name="id_time_update_data" /></div>
	<div id="label">query</div> <div id="field">
		<textarea id="db_query" name="db_query" style="width:500px;height:50px;"></textarea>		
	</div>
	
	<div id="label">regra de conector</div> <div id="field"><input type="text" id="rule_type" name="rule_type" value="sql"/></div>
	
	
	
	
	<input type="button" name="enviar" value="Salvar" onclick="javascript:submitbutton('save');"/>
	<input type="button" name="excluir" value="Excluir" onclick="javascript:submitbutton('delete');"/>
	
	<input type="button" name="cancelar" value="Cancelar" onclick="javascript:window.location.href='view_list.php';"/>
	
</form>


<script>
getUsername();
getItem(getURIParameter('id'));
</script>